/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Employee;

import SalaryComponent.EmpSalDetailsBean;
import SalaryComponent.EmpSalaryBean;
import SalaryComponent.SalaryComponentBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mukesh
 */
 class EmployeeSalary1 extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            int empid = 0;
            if (request.getParameter("empid").length() != 0) {
                empid = Integer.parseInt(request.getParameter("empid"));
            }
            EmpSalaryBean esb = SalaryComponent.Query.viewAllEmpSal(empid);

            Vector<Object> list = SalaryComponent.Query.viewAll();
            Iterator it = list.iterator();
            Iterator it1 = list.iterator();
            StringBuilder stb = new StringBuilder();
            out.println("<div class='details pull-left userprofile'>");
            out.println("<table class='table table-striped table-detail' style='margin-left: 200px;' id='tsal'>");
            out.println("<tr>");
            out.println("<th>Total Earnings</th>");
            out.println("<td style='text-align:right'>" + esb.getTotalEarning() + "</td>");
            out.println("</tr><tr><th>Total Deductions</th>");
            out.println("<td id='deduction' style='text-align:right'>" + esb.getTotalDeduction() + "</td>");
            out.println("</tr><tr><th><span class='label label-inverse'>Total Salary</span></th>");
            out.println("<td  style='text-align:right'>" + esb.getTotalSal() + "</td>");
            out.println("</tr></table></div>");
            out.println("<legend>");
            out.print("Earnings");
            out.println("</legend>");
            out.println("<div class='controls'><table>");

            Vector list1 = SalaryComponent.Query.viewAllEmpSalDetail(esb.getEmpSalId());

            dd:
            while (it.hasNext()) {
                SalaryComponentBean scb = (SalaryComponentBean) it.next();
                stb.append(scb.getSalaryComponentId() + "/" + scb.getValueType() + "-");

                if (scb.getSalaryType() == 1) {
                    for (int i = 0; i < list1.size(); i++) {
                        EmpSalDetailsBean esd = (EmpSalDetailsBean) list1.get(i);
                        if (esd.getSalCompoId() == scb.getSalaryComponentId()) {
                            out.println("<tr><td style='width:30px'><input type='checkbox' checked name='" + scb.getSalaryComponentId() + "-c' id='" + scb.getSalaryComponentId() + "-c-" + scb.getValueType() + "' onclick='enableSalary(this.id,this.checked)'></td><td>" + scb.getSalaryComponent() + "</td>");
                            out.println("</td><td><input type='text' name='"+scb.getSalaryComponentId()+"' class='input-square' title='earning' value='" + esd.getAmount() + "' id='" + scb.getSalaryComponentId() + "-" + scb.getValueType() + "' onblur='manageSal(this.id,this.title,this.value)' onfocus='manageSalOn(this.id,this.title,this.value)' style='margin: 10px;'></td>");
                            if (scb.getValueType() == 2) {
                                out.println("<td>Amount</td></tr>");
                            } else {
                                out.println("<td>%</td></tr>");
                            }
                            continue dd;
                        }
                        out.println("</label>");

                    }

                    out.println("<tr><td style='width:30px'><input type='checkbox' name='" + scb.getSalaryComponentId() + "-c' id='" + scb.getSalaryComponentId() + "-c-" + scb.getValueType() + "' onclick='enableSalary(this.id,this.checked)'></td><td>" + scb.getSalaryComponent() + "</td>");
                    out.println("</td><td><input type='text' name='" + scb.getSalaryComponentId() + "' class='input-square' title='earning' id='"+scb.getSalaryComponentId()+"-"+scb.getValueType()+"' onblur='manageSal(this.id,this.title,this.value)' onfocus='manageSalOn(this.id,this.title,this.value)' style='margin: 10px;' readonly></td>");
                    if (scb.getValueType() == 2) {
                        out.println("<td>Amount</td></tr>");
                    } else {
                        out.println("<td>%</td></tr>");
                    }
                    out.println("</label>");
                }
            }
            out.println("</table></div>");
            out.println("<legend>");
            out.print("Deductions");
            out.println("</legend>");
            out.println("<div class='controls'><table><tr>");

            dd1: while (it1.hasNext()) {
                SalaryComponentBean scb = (SalaryComponentBean) it1.next();
                if (scb.getSalaryType() == 2) {
                    
                    for (int i = 0; i < list1.size(); i++) {
                        EmpSalDetailsBean esd = (EmpSalDetailsBean) list1.get(i);
                        if (esd.getSalCompoId() == scb.getSalaryComponentId()) {
                            out.println("<tr><td style='width:30px'><input type='checkbox' checked name='" + scb.getSalaryComponentId() + "-c' id='" + scb.getSalaryComponentId() + "-c-" + scb.getValueType() + "' onclick='enableSalary(this.id,this.checked)'></td><td>" + scb.getSalaryComponent() + "</td>");
                            out.println("</td><td><input type='text' name='" + scb.getSalaryComponentId() + "' class='input-square' title='deduction' value='" + esd.getAmount() + "' id='" + scb.getSalaryComponentId()+"-"+scb.getValueType()+"' onblur='manageSal(this.id,this.title,this.value)' onfocus='manageSalOn(this.id,this.title,this.value)' style='margin: 10px;'></td>");
                            if (scb.getValueType() == 2) {
                                out.println("<td>Amount</td></tr>");
                            } else {
                                out.println("<td>%</td></tr>");
                            }
                            continue dd1;
                        }
                        out.println("</label>");

                    }

                    out.println("<tr><td style='width:30px'><input type='checkbox' name='" + scb.getSalaryComponentId() + "-c' id='" + scb.getSalaryComponentId() + "-c-" + scb.getValueType() + "' onclick='enableSalary(this.id,this.checked)'></td><td>" + scb.getSalaryComponent() + "</td>");
                    out.println("</td><td><input type='text' name='" + scb.getSalaryComponentId() + "' class='input-square' title='deduction' id='" +scb.getSalaryComponentId()+"-"+scb.getValueType()+"' onblur='manageSal(this.id,this.title,this.value)' onfocus='manageSalOn(this.id,this.title,this.value)' style='margin: 10px;' readonly></td>");
                    if (scb.getValueType() == 2) {
                        out.println("<td>Amount</td></tr>");
                    } else {
                        out.println("<td>%</td></tr>");
                    }
                    out.println("</label>");
                }
            }
            out.println("</table></div>");
            out.println("<input type='hidden' name='noc' id='noc' value='" + stb.toString() + "'>");
            out.println("<input type='hidden' name='tote' id='tote' value='"+esb.getTotalEarning()+"'>");
            out.println("<input type='hidden' name='totd' id='totd' value='"+esb.getTotalDeduction()+"'>");
            out.println("<input type='hidden' name='tota' id='tota' value='"+esb.getTotalSal()+"'>");
            out.println("<div class='form-actions' style='margin: 20px -15px -35px -15px;'>");
            out.println("<input class='btn btn-danger' type='button' onclick=iback('3') value='Back'>");
            out.println("<input class='btn btn-primary' type='submit' value='Submit'>");
            out.println("</div>");

        } catch (Exception e) {
        } finally {
            out.close();
        }
    }

}